package com.cn.wanxi.service;

import com.cn.wanxi.dto.ProductDto;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.MyEnum;
import com.cn.wanxi.util.WriteRead;

import java.util.ArrayList;
import java.util.List;

import static com.cn.wanxi.util.MyPath.FILE_PRODUCT;

public class ProductService {

    public List<ProductModel> getProductModelList() {
        /**
         * 1.写
         * 2.读
         * 3.封装
         */
        writeNote();
        String result = WriteRead.readNote(FILE_PRODUCT);


        return setData(result);
    }

    private void writeNote() {
        ProductDto productDto = new ProductDto();
        WriteRead.writeNote(MyEnum.FILE_PRODUCT.getPath(), productDto.list);
    }


    private List<ProductModel> setData(String result) {
        List<ProductModel> list = new ArrayList<>();
        String[] results = result.split(MyEnum.SPLIT.getPath());
        for (int i = 0; i < results.length; i++) {
            ProductModel model = new ProductModel();
            model.setSrc(results[i]);
            list.add(model);
        }
        return list;
    }
}
